package com.sitech.portal.webHooks;

import com.sitech.ibnms.core.BaseIbnmsAction;
import com.sitech.ibnms.util.JSONUtil;
import com.sitech.portal.system.domain.TblSYS_DEPT;
import com.sitech.portal.system.service.ISysDeptService;
import com.sitech.portal.webHooks.domain.Dept;
import com.sitech.portal.webHooks.domain.HookRequestBody;
import com.sitech.portal.webHooks.domain.WebHooks;
import com.sitech.portal.webHooks.service.IWebHooksService;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: yanran
 * Date: 15-9-11
 * Time: 上午10:19
 * To change this template use File | Settings | File Templates.
 */
public class DeptEventProcessor extends BaseIbnmsAction {
    private IWebHooksService webHooksService;
    private ISysDeptService sysDeptService;
    private static final Log logger = LogFactory.getLog(DeptEventProcessor.class);


    public Object whenAddDept(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        TblSYS_DEPT after = (TblSYS_DEPT)joinPoint.getArgs()[0];

        Dept afterDept = new Dept(after);
        afterDept.setCREATED_BY(this.getCurrentUser().getLOGIN_ID());
        afterDept.setCREATED_AT(EventProcessorUtil.getNowTime());
        Dept beforeDept = null;

        String object_id = after.getDEPT_ID();
        String object_kind = "dept";
        String operate_kind = "create";
        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeDept));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterDept));
            hrb.setObject_id(object_id);

            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                 post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("增加部门同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }
    public Object whenUpdateDept(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        TblSYS_DEPT after = (TblSYS_DEPT)joinPoint.getArgs()[0];
        TblSYS_DEPT before =  sysDeptService.findDepartByPK(after.getDEPT_ID());

        Dept afterDept = new Dept(after);
        afterDept.setUPDATED_BY(this.getCurrentUser().getLOGIN_ID());
        afterDept.setUPDATED_AT(EventProcessorUtil.getNowTime());
        Dept beforeDept = new Dept(before);

        String object_id = after.getDEPT_ID();
        String object_kind = "dept";
        String operate_kind = "update";
        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeDept));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterDept));
            hrb.setObject_id(object_id);


            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("修改部门同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }

    public Object whenDeleteDept(ProceedingJoinPoint joinPoint) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException {
        TblSYS_DEPT dept = (TblSYS_DEPT)joinPoint.getArgs()[0];
        TblSYS_DEPT before = sysDeptService.findDepartByPK(dept.getDEPT_ID());


        String object_id = before.getDEPT_ID();
        String object_kind = "dept";
        String operate_kind = "delete";

        Dept beforeDept =  new Dept(before);
        Dept afterDept =  new Dept();
        afterDept.setDEPT_ID(object_id);
        afterDept.setDELETED_BY(this.getCurrentUser().getLOGIN_ID());
        afterDept.setDELETED_AT(EventProcessorUtil.getNowTime());

        Object o = null;
        try {
            o = joinPoint.proceed();
            //调用接口
            List<WebHooks>  list= webHooksService.selectWebHooksUrl(object_kind);
            HookRequestBody hrb = new HookRequestBody();
            hrb.setObject_kind(object_kind);
            hrb.setOperate_kind(operate_kind);
            hrb.setObject_before(EventProcessorUtil.toJSONWithOutNull(beforeDept));
            hrb.setObject_after(EventProcessorUtil.toJSONWithOutNull(afterDept));
            hrb.setObject_id(object_id);

            for(WebHooks os : list){
                String url = os.getURL();
                PostMethod post = new PostMethod(url);
                 post.setRequestBody(JSONUtil.toJSON(hrb));
                try {
                    new HttpClient().executeMethod(post);
                }catch (Exception e){
                    logger.error(e);
                }
                logger.info("删除部门同步接口:"+url+"   status:"+post.getStatusCode()+"  return:"+post.getResponseBodyAsString());
            }
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return o;
    }

    public void setWebHooksService(IWebHooksService webHooksService) {
        this.webHooksService = webHooksService;
    }

    public void setSysDeptService(ISysDeptService sysDeptService) {
        this.sysDeptService = sysDeptService;
    }

}
